<?php

namespace App\Admin\Controllers;

use App\Models\PetProductCate;
use App\Models\PetCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Tree;
use Dcat\Admin\Http\Controllers\AdminController;

class PetProductCateController extends AdminController
{

    /**
     * @return mixed
     * 获取表单查询父类所需要的数组
     */
    public function getCate(){
        return PetProductCate::select(['id','title as text'])->get();
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PetProductCate(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('parent_id','父级')->display(function ($parent_id){
                if ($parent_id){
                    $data = \App\Models\PetProductCate::find($parent_id);
                    return $data->title;
                }else{
                    return '顶级';
                }

            });
            $grid->column('title')->editable(true);
//            $grid->column('describe');
            $grid->column('image')->image();
            $grid->column('order')->sortable()->editable(true);
            $grid->column('is_show')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

//            快速搜索
//            $grid->quickSearch('title')->placeholder('输入名称');

//数据导出
            $grid->export()->rows(function (array $rows) {
                foreach ($rows as $index => &$row) {
                    if ($row['parent_id']){
                        $data = PetProductCate::find($row['parent_id']);
                        $row['parent_id'] = $data->title;
                    }else{
                        $row['parent_id'] = '顶级';
                    }
                    if ($row['is_show']){
                        $row['is_show'] = '显示';
                    }else{
                        $row['is_show'] = '下架';
                    }

                }

                return $rows;
            });
            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                // 展开过滤器
                $filter->expand();


                $filter->equal('id')->width(4);
                $filter->equal('parent_id','父类')
                    ->select('productCategory')->width(4);
                $filter->like('title')->width(6);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PetProductCate(), function (Show $show) {
            $show->field('id');
            $show->field('parent_id');
            $show->field('title');
            $show->field('describe');
            $show->image()->image();
            $show->field('order');
            $show->is_show()->as(function ($is_show) {
                if ($is_show){
                    return "显示";
                }else{
                    return "不显示";
                }
            });
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PetProductCate(), function (Form $form) {
            $form->display('id');
            $form->select('parent_id', trans('admin.parent_id'))->options(function ()  {
                return PetProductCate::selectOptions();
            })->required();
            $form->text('title');
            $form->text('describe');
            $form->image('image');
            $form->text('order')->default(1);
            $form->radio('is_show')->options([0 => '不显示', 1=> '显示'])->default(0);
            $form->display('created_at');
            $form->display('updated_at');
            $form->map('latitude', 'longitude', '选择坐标');
        });
    }

}
